package com.solver;

import com.editor.utils.generator.GeneratorModel;
import com.ismaplus.intg.cauchy.CauchyProblem;
import com.ismaplus.intg.methods.IntegrationMethod;
import com.ismaplus.intg.methods.Solution;
import com.ismaplus.intg.seqlib.IntegrationMethodFactory;
import com.ismaplus.intg.seqlib.IntegrationMethodType;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Created by Arseniy Nazarov on 16.10.14.
 */
public class SolverManager {

    public static Solution solve(Object[] cells) {

        CauchyProblem cauchyProblem = null;

        String modelName = "NewModel";

        GeneratorModel generatorModel = new GeneratorModel(
                modelName,
                cells,
                "0",  //beginInterval
                "50", //endInterval
                "0.5"); //stepSize

        URL url = null;
        try {
            url = new URL("file:///");
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        URL[] classUrls = {url};
        URLClassLoader ucl = new URLClassLoader(classUrls);
        try {
            Class modelClass = ucl.loadClass(modelName);
            Object object = modelClass.newInstance();
            cauchyProblem = (CauchyProblem) object;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        IntegrationMethodType integrationMethodType = IntegrationMethodType.EULER;

        IntegrationMethod integrationMethod = IntegrationMethodFactory.createMethod(integrationMethodType);
        Solution solution = integrationMethod.solve(cauchyProblem);
        return solution;
    }

}
